/*PhotoGalleryRejected.java
 * Generates the Rejected photos photo gallery
 * 
 * Gallery code based on http://developer.android.com/resources/tutorials/views/hello-gridview.html
 * Alert dialog based on example from http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/AlertDialogSamples.html
 * Toast code based on example from http://developer.android.com/guide/topics/ui/notifiers/toasts.html
 */
package com.tagit;


import com.tagit.PhotosDbAdapter;
import com.tagit.PhotoCursorAdapter;
import com.tagit.MyApp;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Toast;



public class PhotoGalleryRejected extends Activity{
	
	private static final int ADD_TAG_ID = 0;
	private PhotosDbAdapter mPhotoDb;
	private static PhotoCursorAdapter mCursorAdapter;
	public static GridView gridview;
	public static int selectState; // Track state of select button
	public static final String PREFS_NAME = "MyPrefsFile";
	Button btGoBack;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    mPhotoDb = new PhotosDbAdapter(this);
        mPhotoDb.open();
        Cursor c = mPhotoDb.selectRejected(30);
	    setContentView(R.layout.photogalleryrejected);
	    
	    gridview = (GridView) findViewById(R.id.gridview);
	    selectState = 0;
	    
	    //Creating a brand new photoAdapter
	    mCursorAdapter = new PhotoCursorAdapter(this, c, selectState);
	    MyApp.setAdapter(mCursorAdapter);
	    gridview.setAdapter(mCursorAdapter);
	    registerForContextMenu(gridview);
	    
	    btGoBack = (Button) findViewById(R.id.goBack);
	    
	    
	    //Set Listener for user clicking on photo (pulled out poorly)
	    setPicListener();
	    
	    //Set Listener for user clicking on buttons
	    setButtonClickListener();
	    
		btGoBack.setOnClickListener(new View.OnClickListener(){
			public void onClick(View v) {
				Intent intent = new Intent(PhotoGalleryRejected.this, PhotoGallery.class);
				startActivity(intent);
			}
		});
	    
	}
	
	@Override
	protected void onResume(){
		super.onResume();

		// Reset the cursor
		Cursor c = mPhotoDb.selectRejected(30);
		mCursorAdapter.setState(0); // Not in select mode
		mCursorAdapter = new PhotoCursorAdapter(this, c, selectState);
		MyApp.setAdapter(mCursorAdapter);
		gridview.setAdapter(mCursorAdapter);

		// Reset the listeners
		setPicListener();
		setButtonClickListener();

	}
	
    
    private void CreateMenu(Menu menu)
    {
        menu.setQwertyMode(true);
        MenuItem mnu1 = menu.add(0, 0, 0, "Preferences");
        {
            mnu1.setAlphabeticShortcut('a');            
        }
        MenuItem mnu2 = menu.add(0, 1, 1, "Help");
        {
            mnu2.setAlphabeticShortcut('b');       
        }
    }
 
    private boolean MenuChoice(MenuItem item)
    {        
        switch (item.getItemId()) {
        case 0:
            goToPreferences();
            return true;
        case 1:
            goToHelp();
            return true;         
        }
        return false;
    } 
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        CreateMenu(menu);
        return true;
    }
 
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {    
         return MenuChoice(item);    
    }
    
    
    
    public void goToPreferences(){
    	Intent intent = new Intent(PhotoGalleryRejected.this, Preferences.class);	
		startActivity(intent);	
    }
    
    public void goToHelp(){
    	Intent intent = new Intent(PhotoGalleryRejected.this, Help.class);	
		startActivity(intent);
    }
    
    public void setPicListener(){
    	gridview.setOnItemClickListener(new OnItemClickListener() {
	        //@Override
			public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
	           if (selectState == 0){
	        	   Intent intent = new Intent(PhotoGalleryRejected.this, ViewRejectedPhoto.class);	
	        	   Bundle extras = new Bundle();
	        	   extras.putString(PhotosDbAdapter.KEY_URI, (String) mCursorAdapter.getItem(position));
	        	   extras.putLong(PhotosDbAdapter.KEY_ID, mCursorAdapter.getItemId(position));
	        	   intent.putExtras(extras);
				
	        	   String imagePosition = "pos";
				
	        	   intent.putExtra(imagePosition, position);
	        	   startActivity(intent);
	           }
	           else{
	        	   if(!(mCursorAdapter.isSelected(mCursorAdapter.getItemId(position)))){
	        		   mCursorAdapter.addSelected(mCursorAdapter.getItemId(position));
	        	   		gridview.setAdapter(mCursorAdapter);
	        	   } else{
	        		   mCursorAdapter.removeSelected(mCursorAdapter.getItemId(position));
	        	   		gridview.setAdapter(mCursorAdapter);
	        	   }
	           }
	        }
	    });
    	
    	gridview.setOnItemLongClickListener(
        		new AdapterView.OnItemLongClickListener() {
    				public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
    						int arg2, long arg3) {
    					
    					mCursorAdapter.addSelected(arg3);
    					
    					Intent intent = new Intent(arg0.getContext(), SelectModeRejected.class);			
    					startActivity(intent);
    					
    					//Toast message to let know the user he is on select mode
    					Context context = getApplicationContext();
    					CharSequence text = "Entering unreject select mode";
    					int duration = Toast.LENGTH_LONG;
    					Toast toast = Toast.makeText(context, text, duration);
    					toast.show();
    					return true;
    				}
        	});
    }
    
    // Method for when a user clicks on Select button
    public void setButtonClickListener(){
    	Button selectButton = (Button) findViewById (R.id.select);
    	selectButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
					Intent intent = new Intent(v.getContext(), SelectModeRejected.class);			
					startActivity(intent);
					
					//Toast message to let know the user he is on select mode
					Context context = getApplicationContext();
					CharSequence text = "Entering unreject select mode";
					int duration = Toast.LENGTH_LONG;
					Toast toast = Toast.makeText(context, text, duration);
					toast.show();

			}
		});
    }
    
    public static void setSelectState(int state){
    	mCursorAdapter.setState(state);
    }
    
    public static PhotoCursorAdapter getAdapter (){
    	return mCursorAdapter;
    }


}
